

package com.bei_en.teacher.admin.controller.questions;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.questions.bean.QuestionsArticle;
import com.bei_en.teacher.admin.model.questions.condition.QuestionsArticleCondition;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsArticleVo;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsVo;
import com.bei_en.teacher.admin.service.questions.QuestionsArticleService;
import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.JsonResult;
import com.bei_en.common.web.JsonSuccessResult;
import com.mysql.fabric.xmlrpc.base.Array;
/**
 * 
 * @author Guocg
 * @date 2016-8-21 21:15:33
 */
@Controller
@RequestMapping("/api/QuestionsArticle")
public class QuestionsArticleAPPController extends BaseController {

    @Autowired
    QuestionsArticleService questionsArticleService;


    @RequestMapping("/questionsArticle")
	@ResponseBody
    public JsonResult questionsArticle(Model model,@Valid  QuestionsArticleCondition vo, BindingResult br, HttpSession session ) {
    	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
    	
     	try {
     		
     	   List<QuestionsArticleVo> list =new ArrayList<QuestionsArticleVo>();
		   list = questionsArticleService.findQuestionsArticlePage(vo);
		   JSONArray array=new JSONArray();
		   JSONObject object=null;
		   QuestionsArticleVo questionsArticleVo=null;
		   for (int i = 0; i < list.size(); i++) {
			   object=new JSONObject();
			   questionsArticleVo=list.get(i);
	   object.put("id",  questionsArticleVo.getId());
	   object.put("title",  questionsArticleVo.getTitle());
	   object.put("content",  questionsArticleVo.getContent());
	   object.put("questionsType",  questionsArticleVo.getQuestionsType());
	   object.put("articleType",  questionsArticleVo.getArticleType());
	   object.put("createTime",  questionsArticleVo.getCreateTime());
			   array.add(object);
		}
		return new JsonSuccessResult(session.getId(), array);
		
     	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }
	 
    
    @RequestMapping("/questionsArticleAdd")
    @ResponseBody
    public JsonResult questionsArticleAdd(Model model,@Valid  QuestionsArticleCondition vo, BindingResult br, HttpSession session) {
      	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
      	try {
      		
      	
		    QuestionsArticleVo questionsArticle=new QuestionsArticleVo();
			questionsArticleService.insert(questionsArticle);
			return new JsonSuccessResult(session.getId(), "成功");
      	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }

    @RequestMapping("/questionsArticleEdit")
    public JsonResult questionsArticleEdit(Model model,@Valid  QuestionsArticleCondition vo, BindingResult br, HttpSession session) {
      	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
      	
    	try {
    	QuestionsArticleVo questionsArticle=new QuestionsArticleVo();
	
		questionsArticleService.updateSelective(questionsArticle);
	
		return new JsonSuccessResult(session.getId(), "成功");
		
     	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }

  
    
}
